package cn.lucode.fastdev.user.controller;

import cn.lucode.fastdev.common.CommonBizTypeCode;
import cn.lucode.fastdev.common.CommonResponseModel;
import cn.lucode.fastdev.user.model.LoginReq;
import cn.lucode.fastdev.user.model.RegisterReq;
import cn.lucode.fastdev.user.service.UserService;
import cn.lucode.fastdev.user.utils.UserException;
import cn.lucode.fastdev.util.LogUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author yunfeng.lu
 * @create 2017/12/4.
 */
@RestController
@RequestMapping("/user")
public class UserController {

    private static final Logger logger = LoggerFactory.getLogger(UserController.class);
    @Autowired
    UserService userService;

    @PostMapping("/login")
    public Object login(LoginReq loginReq) {

        try {

            return CommonResponseModel.success(userService.login(loginReq));

        } catch (UserException ue) {

            LogUtil.error(logger, ue, "{0} 异常，请检查....{1}", CommonBizTypeCode.BIZ_USER.getDesc(), loginReq);

            return CommonResponseModel.facade(ue.getException_type());

        } catch (Exception ex) {

            LogUtil.error(logger, ex, "{0} 异常，请检查....{1}", CommonBizTypeCode.BIZ_USER.getDesc(), loginReq);

            return CommonResponseModel.fail();
        }

    }

    @PostMapping("/registerStep1")
    public Object register1(RegisterReq registerReq) {
        try {
            return CommonResponseModel.success(userService.registerStep1(registerReq));

        } catch (UserException ue) {

            LogUtil.error(logger, ue, "{0} 异常，请检查....{1}", CommonBizTypeCode.BIZ_USER.getDesc(), registerReq);

            return CommonResponseModel.facade(ue.getException_type());

        } catch (Exception ex) {

            LogUtil.error(logger, ex, "{0} 异常，请检查....{1}", CommonBizTypeCode.BIZ_USER.getDesc(), registerReq);

            return CommonResponseModel.fail();
        }
    }

    @PostMapping("/registerStep2")
    public Object register2(RegisterReq registerReq) {
        try {
            userService.registerStep2(registerReq);
            return CommonResponseModel.success();

        } catch (UserException ue) {

            LogUtil.error(logger, ue, "{0} 异常，请检查....{1}", CommonBizTypeCode.BIZ_USER.getDesc(), registerReq);

            return CommonResponseModel.facade(ue.getException_type());

        } catch (Exception ex) {

            LogUtil.error(logger, ex, "{0} 异常，请检查....{1}", CommonBizTypeCode.BIZ_USER.getDesc(), registerReq);

            return CommonResponseModel.fail();
        }
    }

    @GetMapping("/getUserInfo")
    public Object getUserInfo(
        @RequestParam(value = "authToken") String authToken) {
        try {

            return CommonResponseModel.success(userService.getUserInfoByAuthToken(authToken));

        } catch (UserException ue) {

            LogUtil.error(logger, ue, "{0} 异常，请检查....{1}", CommonBizTypeCode.BIZ_USER.getDesc(), authToken);

            return CommonResponseModel.facade(ue.getException_type());

        } catch (Exception ex) {

            LogUtil.error(logger, ex, "{0} 异常，请检查....{1}", CommonBizTypeCode.BIZ_USER.getDesc(), authToken);

            return CommonResponseModel.fail();
        }

    }

    @GetMapping("/layout")
    public Object layout(
        @RequestParam(value = "authToken") String authToken) {
        try {
            userService.layout(authToken);
            return CommonResponseModel.success();

        } catch (UserException ue) {

            LogUtil.error(logger, ue, "{0} 异常，请检查....{1}", CommonBizTypeCode.BIZ_USER.getDesc(), authToken);

            return CommonResponseModel.facade(ue.getException_type());

        } catch (Exception ex) {

            LogUtil.error(logger, ex, "{0} 异常，请检查....{1}", CommonBizTypeCode.BIZ_USER.getDesc(), authToken);

            return CommonResponseModel.fail();
        }

    }

}
